package com.pxxy.springmvc.controller;

import com.pxxy.springmvc.entity.Book;
import com.pxxy.springmvc.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/******************
 * @Author yejf
 * @Description TODO
 */
@RestController  //  它相当于 @Controller + @ResponseBody
@RequestMapping("/rest")
public class RestfulController {

    @Autowired
    private BookService bookService;

    @RequestMapping(value = "/book/{id}", method = RequestMethod.GET,
                    produces = "application/json;charset=utf-8")
    public Book getBookById(@PathVariable("id") Integer id) {
        //
        System.out.println(" 进入了 getBookById方法："+id);
        //
        return this.bookService.findById(id);
    }

    @RequestMapping(value = "/book", method = RequestMethod.GET,
                produces = "application/json")
    public List<Book> getAll() {
        //
        System.out.println(" --- 进入了 getAll() 方法");
        return this.bookService.findAll();
    }

    @RequestMapping(value = "/book", method = RequestMethod.POST,
                    produces = "application/json")
    public Book save(@RequestBody Book book) {
        System.out.println("book = " + book);
        //
        return book;
    }
}
